package com.hksj.app.service;

import com.alibaba.fastjson.JSONObject;
import com.hksj.app.websocket.MessageType;
import com.hksj.app.websocket.WebsocketConnectionHolder;
import com.hksj.app.websocket.WebsocketResponse;
import com.hksj.common.quartz.entity.CronJob;
import com.hksj.common.quartz.entity.CronJobDevice;
import com.hksj.common.quartz.service.CronJobDeviceService;
import com.hksj.common.quartz.service.CronJobService;
import com.hksj.drcs.service.DrcsCockpitService;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CronToSwitchService {
    @Autowired
    private DrcsCockpitService drcsCockpitService;
    @Autowired
    private CronJobDeviceService cronJobDeviceService;
    @Autowired
    private CronJobService cronJobService;

    public void otherRoomSwitch(JobDataMap jobDataMap){
        long jobId = jobDataMap.getLong("jobId");
        CronJob cronJob = cronJobService.getById(jobId);
        List<CronJobDevice> deviceList = cronJobDeviceService.lambdaQuery()
                .eq(CronJobDevice::getJobId, jobId)
                .list();
        if (ObjectUtils.isEmpty(deviceList)){
            return;
        }
        List<String> deviceIdList = deviceList.stream().map(CronJobDevice::getDeviceId).collect(Collectors.toList());
        for (String id : deviceIdList) {
            try {
                String deviceId = "";
                String functionId = "";
                String identification = "";
                Integer value = 1;
                int lastUnderscoreIndex = id.lastIndexOf('_');
                if (lastUnderscoreIndex != -1) {
                    // 分割为两部分
                    String part1 = id.substring(0, lastUnderscoreIndex);
                    String part2 = id.substring(lastUnderscoreIndex + 1);
                    deviceId = part1;
                    functionId = "D" + part2;
                }
                int firstUnderscoreIndex = id.indexOf('_');
                if (firstUnderscoreIndex != -1) {
                    // 截取第一个下划线之后的内容
                    String part3 = id.substring(firstUnderscoreIndex + 1);
                    identification = part3 + "_RUN";
                    if (cronJob.getType() == 1){
                        value = 1;
                    }else if (cronJob.getType() == 2){
                        value = 2;
                    }
                }
                HashMap<String, Object> properties = new HashMap<>();
                properties.put(identification,value);
                drcsCockpitService.control(deviceId,functionId,properties);
            }catch (Exception e){
                System.out.println("辅房定时任务：" + jobId + " 执行失败设备id：" + id);
            }
        }
    }

    public void operatingRoomSwitch(JobDataMap jobDataMap){
        long jobId = jobDataMap.getLong("jobId");
        CronJob cronJob = cronJobService.getById(jobId);
        List<CronJobDevice> deviceList = cronJobDeviceService.lambdaQuery()
                .eq(CronJobDevice::getJobId, jobId)
                .list();
        if (ObjectUtils.isEmpty(deviceList)){
            return;
        }
        //任务类型 11 = 开空调  12 = 开灯  21 = 关空调 22 = 关灯
        List<String> functionCodeList = new ArrayList<>();
        switch (cronJob.getType()){
            case 11:
                functionCodeList.add("AC_POWER_ON");
                break;
            case 12:
                functionCodeList.add("LIGHT_POWER_ON1");
                functionCodeList.add("LIGHT_POWER_ON2");
                break;
            case 21:
                functionCodeList.add("AC_POWER_OFF");
                break;
            case 22:
                functionCodeList.add("LIGHT_POWER_OFF1");
                functionCodeList.add("LIGHT_POWER_OFF2");
                break;
        }
        if (ObjectUtils.isEmpty(functionCodeList)){
            return;
        }
        WebsocketResponse websocketResponse = new WebsocketResponse();
        websocketResponse.setCode(HttpStatus.OK.value());
        websocketResponse.setType(MessageType.EQ_CONTROL.value());
        List<String> deviceIdList = deviceList.stream().map(CronJobDevice::getDeviceId).collect(Collectors.toList());
        for (String functionCode : functionCodeList) {
            JSONObject json = new JSONObject();
            json.put("functionCode",functionCode);
            websocketResponse.setData(json);
            WebsocketConnectionHolder.sendMessage(deviceIdList,WebsocketConnectionHolder.toObjString(websocketResponse));
        }
    }
}
